package com.ruoyi.web.controller.V2.submitAccount;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfoang;
import com.ruoyi.system.model.domain.Finance;
import com.ruoyi.system.model.domain.OrderRec;
import com.ruoyi.system.V2.submitAccount.service.IOrderRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 销售订单记录Controller
 *
 * @author ruoyi
 * @date 2022-10-28
 */
@RestController
@RequestMapping("/account/order/rec")
public class OrderRecController extends BaseController {
    @Autowired
    private IOrderRecService orderRecService;

    /**
     * 查询销售订单记录列表
     */
    @GetMapping("/list")
    public TableDataInfoang list(OrderRec orderRec) {
        startPage();
        List<OrderRec> list = orderRecService.selectOrderRecList(orderRec);
        return getDataTableRang(list, orderRec);
    }


    @GetMapping("/createList")
    public AjaxResult createList() {
        List<Map<String, Object>> list = orderRecService.createList();
        return AjaxResult.success(list);
    }

    private TableDataInfoang getDataTableRang(List<OrderRec> list, OrderRec orderRec) {

        TableDataInfoang rspData = new TableDataInfoang();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());

        Map<String, Object> result = orderRecService.sumPrice(orderRec);
        rspData.setParams(result);
        return rspData;
    }

    /**
     * 导出销售订单记录列表
     */
    @Log(title = "销售订单记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrderRec orderRec) {
        List<OrderRec> list = orderRecService.selectOrderRecList(orderRec);
        ExcelUtil<OrderRec> util = new ExcelUtil<OrderRec>(OrderRec.class);
        return util.exportExcel(list, "销售订单记录数据");
    }

    /**
     * 获取销售订单记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(orderRecService.selectOrderRecById(id));
    }

    /**
     * 新增销售订单记录
     */
    @Log(title = "销售订单记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderRec orderRec) {
        return toAjax(orderRecService.insertOrderRec(orderRec));
    }

    /**
     * 新增销售订单记录
     */
    @Log(title = "销售订单记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addTwo")
    public AjaxResult addTwo(@RequestBody OrderRec orderRec) {
        return toAjax(orderRecService.insertOrderRecTwo(orderRec));
    }

    /**
     * 转自售订单记录同步财务
     */
    @Log(title = "转自售订单记录同步财务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/selfSale")
    public AjaxResult selfSale(@RequestBody Finance finance) {
        return toAjax(orderRecService.selfSale(finance));
    }


    /**
     * 修改销售订单记录
     */
    @Log(title = "销售订单记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderRec orderRec) {
        return toAjax(orderRecService.updateOrderRec(orderRec));
    }

    /**
     * 修改销售订单记录
     */
    @Log(title = "销售订单记录", businessType = BusinessType.UPDATE)
    @PutMapping("/bindStatus")
    public AjaxResult bindStatus(@RequestBody OrderRec orderRec) {
        orderRec.setCreateId(getUserId());
        return toAjax(orderRecService.bindStatus(orderRec));
    }

    /**
     * 删除销售订单记录
     */
    @Log(title = "销售订单记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderRecService.deleteOrderRecByIds(ids));
    }
}
